package com.nuorui.module.system.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.extra.servlet.ServletUtil;
import com.nuorui.common.constant.RequestConst;
import com.nuorui.common.core.annoation.NoNeedLogin;
import com.nuorui.common.core.domain.ResponseDTO;
import com.nuorui.common.utils.SmartRequestUtil;
import com.nuorui.module.system.domain.form.LoginForm;
import com.nuorui.module.system.domain.vo.LoginResultVO;
import com.nuorui.module.system.service.LoginService;
import com.nuorui.module.system.utils.AdminRequestUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 员工登录
 *
 * @Author 1024创新实验室-主任:卓大
 * @Date 2021-12-15 21:05:46
 * @Wechat zhuoda1024
 * @Email lab1024@163.com
 * @Copyright <a href="https://1024lab.net">1024创新实验室</a>
 */
@RestController
public class LoginController {

    @Resource
    private LoginService loginService;


    @NoNeedLogin
    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录")
    public ResponseDTO<LoginResultVO> login(@Valid @RequestBody LoginForm loginForm, HttpServletRequest request) {
        String ip = ServletUtil.getClientIP(request);
        String userAgent = ServletUtil.getHeaderIgnoreCase(request, RequestConst.Header.USER_AGENT);
        return loginService.login(loginForm, ip, userAgent);
    }

    @GetMapping("/login/getLoginInfo")
    @ApiOperation(value = "获取登录结果信息", notes = "获取登录结果信息")
    public ResponseDTO<LoginResultVO> getLoginInfo() {
        String tokenValue = StpUtil.getTokenValue();
        LoginResultVO loginResult = loginService.getLoginResult(AdminRequestUtil.getLoginUser(), tokenValue);
        loginResult.setToken(tokenValue);

        return ResponseDTO.ok(loginResult);
    }

//    @NoNeedLogin
//    @GetMapping("/login/getCaptcha")
//    @ApiOperation(value = "获取验证码", notes = "获取验证码")
//    public ResponseDTO<CaptchaVO> getCaptcha() {
//        return loginService.getCaptcha();
//    }

    @GetMapping("/login/logout")
    @ApiOperation(value = "退出登录", notes = "退出登录")
    public ResponseDTO<String> logout() {
        return loginService.logout(SmartRequestUtil.getRequestUser());
    }
}
